<?php

namespace Home\Controller;

class VerifyController extends HomeController
{

    public function __construct()
    {
        parent::__construct();
    }



    public function sendEmailCode()
    {
        $input = I('post.');

        if (!check($input['email'], 'email')) {
            $this->ajaxError('邮箱格式错误');
        }

        $user = M('User')->where(array(
            'email' => $input['email']
        ))->find();
        if ($user) {
            $this->ajaxError('邮箱已存在');
        }

//        if ($input['type']){
//            if (!$user){
//                $this->ajaxError('没有此邮箱');
//            }
//        }

        $code = rand(111111, 999999);
        $content = '您的验证码为: ' . $code;
        $result = sendMail($input['email'] ,'',$content);
        if ($result){
            //$checkmsg = $input['email'] . $code . ':time' . time();
            //$res = file_put_contents('Public/code/'.$input['email'] .'.txt',$checkmsg);
//            $addtime = M()->where(array(
//                'email' =>
//            ))->getField('addtime');
            $rs = M('EmailCode')->add(array(
                'email' => $input['email'],
                'code' => $code,
                'addtime' => time()
            ));

            if ($rs){
                $this->ajaxSuccess('邮箱验证码已发送到您的邮箱');
            }else{
                $this->ajaxError('邮箱验证码发送失败,请重新点击发送');
            }
        }
    }

    public function findpwdEmailCode(){
        $input = I('post.');

        if (!check($input['email'], 'email')) {
            $this->error(L('邮箱格式错误'));
        }

        $user = M('User')->where(array(
            'email' => $input['email']
        ))->find();

        if (!$user){
            $this->ajaxError('您输入的邮箱不存在');
        }

        $code = rand(111111, 999999);
        $content = '您的验证码为：' . $code;

        $result = sendMail($input['email'] ,'',$content);
        if ($result){
            $checkmsg = $input['email'] . $code . ':time' . time();
            $res = file_put_contents('Public/code/'.$input['email'] .'.txt',$checkmsg);
            if ($res){
                $this->ajaxSuccess('邮箱验证码已发送到您的邮箱');
            }else{
                $this->ajaxError('邮箱验证码发送失败,请重新点击发送');
            }
        }
    }
}

?>